package top.yokey.learn1;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> {

    private OnItemClickListener onItemClickListener;
    private List<MainBean.CommentsDTO> arrayList = new ArrayList<>();

    public MainAdapter(List<MainBean.CommentsDTO> arrayList) {
        this.arrayList = arrayList;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        MainBean.CommentsDTO bean = arrayList.get(position);
        holder.mainTextView.setText(bean.getUser().getNickname());
        holder.contentTextView.setText(bean.getText());

        holder.mainRelativeLayout.setOnClickListener(view -> {
            if (onItemClickListener != null) {
                onItemClickListener.onClick(position, bean);
            }
        });

    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list_main1, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {

        this.onItemClickListener = onItemClickListener;

    }

    public interface OnItemClickListener {

        void onClick(int position, MainBean.CommentsDTO dto);

    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private final RelativeLayout mainRelativeLayout;
        private final AppCompatTextView mainTextView;
        private final AppCompatTextView contentTextView;

        public ViewHolder(@NonNull View view) {
            super(view);
            mainRelativeLayout = view.findViewById(R.id.mainRelativeLayout);
            mainTextView = view.findViewById(R.id.mainTextView);
            contentTextView = view.findViewById(R.id.contentTextView);
        }
    }

}
